//
//  HBScoreController.m
//  HeartBeat
//
//  Created by karong on 2023/9/18.
//

#import "HBScoreController.h"

@interface HBScoreController ()
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *scoreBtnArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *scoreBtn;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, assign) NSInteger score;
@end

@implementation HBScoreController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = self.novel.name;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.novel.thumb]];
}

- (void)addScoreRequest:(NSInteger)score {
    [HBRequest addScoreWithNovel_Id:self.novel.book_id score:score success:^{
        [HBToast simple:WSLocalized(@"評分提交成功啦~")];
        [self.navigationController popViewControllerAnimated:YES];
    } failed:^(NSURLResponse *response, NSError *error, id responseObject) {
        
    }];
}

- (IBAction)scoreAction:(UIButton *)sender {
    self.score = sender.tag;
}

- (IBAction)addScoreAction:(id)sender {
    if (self.score > 0) {
        [self addScoreRequest:self.score];
    }
    else {
        [HBToast simple:WSLocalized(@"最低為1顆星")];
    }
}

- (void)setScore:(NSInteger)score {
    _score = score;
    for (UIButton * btn in self.scoreBtnArray) {
        btn.selected = btn.tag <= score;
    }
}
@end
